package cn.yuli.redis.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Service;

import cn.yuli.redis.service.CacheService2;
/**
 * redis缓存服务类
 * @author yuli
 *
 */
@Service
public class RedisCacheServiceImpl implements CacheService2 {
	@Autowired
	private CacheManager cm;//导入缓存管理器
	/**
	 * 获取缓存结果
	 */
	@SuppressWarnings("unchecked")
	public <T> T cacheResult(String key, String cacheName) {
		ValueWrapper valueWrapper = cm.getCache(cacheName).get(key);
		return (T) (valueWrapper == null ? null:valueWrapper.get());
	}
	/**
	 * 移除缓存
	 */
	public void cacheRemove(String key, String cacheName) {
		cm.getCache(cacheName).evict(key);
	}
	/**
	 * 添加缓存
	 */
	public <T> void cachePut(String key, T value, String cacheName) {
		cm.getCache(cacheName).put(key, value);
	}

}
